package com.fjwt.gz.service.other.export;

import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.db.dto.ActivityAwardDTO;
import com.fjwt.gz.db.entity.ActivityAwardEntity;
import com.fjwt.gz.db.entity.ActivityEntity;
import com.fjwt.gz.db.entity.AwardSettingEntity;
import com.fjwt.gz.db.entity.SysExportTaskEntity;
import com.fjwt.gz.db.vo.ExportAwardVO;
import com.fjwt.gz.service.activity.AbstractExportReport;
import com.fjwt.gz.service.impl.ActivityAwardService;
import com.fjwt.gz.service.impl.ActivityService;
import com.fjwt.gz.service.impl.AwardSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ActivityBoardExportReport extends AbstractExportReport<ExportAwardVO> {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityAwardService activityAwardService;
    @Autowired
    private AwardSettingService awardSettingService;


    private static final Map<String, String> headerCouponMap = new HashMap<>();

    static {
        headerCouponMap.put("fansId", "用户唯一ID");
        headerCouponMap.put("realName", "参与客户姓名");
        headerCouponMap.put("phone", "参与客户手机号");
        headerCouponMap.put("isAuth", "参与客户是否实名");
        headerCouponMap.put("couponName", "领用卡券名称");
        headerCouponMap.put("couponPrice", "卡券金额");
        headerCouponMap.put("couponThirdId", "微信唯一卡券编号");
        headerCouponMap.put("createdAt", "参与时间");
        headerCouponMap.put("writeTime", "核销时间");
        headerCouponMap.put("status", "卡券状态");
        headerCouponMap.put("managerAgentNo", "发放客户经理归属机构号");
        headerCouponMap.put("managerAgentNoName", "发放客户经理归属网点");
        headerCouponMap.put("tallerManagerAgentNo", "发放客户经理归属区县机构号");
        headerCouponMap.put("tallerManagerAgentNoName", "发放客户经理归属区县");
        headerCouponMap.put("managerAgentName", "发放人姓名");
        headerCouponMap.put("managerAgentPhone", "发放人手机号");
    }

    private static final Map<String, String> headerGoodsMap = new HashMap<>();

    static {
        headerGoodsMap.put("fansId", "用户唯一ID");
        headerGoodsMap.put("realName", "参与客户姓名");
        headerGoodsMap.put("phone", "参与客户手机号");
        headerGoodsMap.put("isAuth", "参与客户是否实名");
        headerGoodsMap.put("goodsName", "奖品名称");
        headerGoodsMap.put("createdAt", "参与时间");
        headerGoodsMap.put("writeTime", "核销时间");
        headerGoodsMap.put("status", "实物状态");
    }

    private static final Map<String, String> headerVirtualMap = new HashMap<>();

    static {
        headerVirtualMap.put("fansId", "用户唯一ID");
        headerVirtualMap.put("realName", "参与客户姓名");
        headerVirtualMap.put("phone", "参与客户手机号");
        headerVirtualMap.put("isAuth", "参与客户是否实名");
        headerVirtualMap.put("goodsName", "奖品名称");
        headerVirtualMap.put("createdAt", "参与时间");
        headerVirtualMap.put("writeTime", "核销时间");
        headerVirtualMap.put("status", "虚拟奖品状态");
    }

    private static final Map<String, String> headerScorcMap = new HashMap<>();

    static {
        headerScorcMap.put("fansId", "用户唯一ID");
        headerScorcMap.put("realName", "参与客户姓名");
        headerScorcMap.put("phone", "参与客户手机号");
        headerScorcMap.put("isAuth", "参与客户是否实名");
        headerScorcMap.put("score", "积分值");
        headerScorcMap.put("name", "积分名称");
        headerScorcMap.put("couponThirdId", "微信唯一卡券编号");
        headerScorcMap.put("createdAt", "参与时间");
        headerScorcMap.put("writeTime", "核销时间");
        headerScorcMap.put("status", "积分状态");
    }

    private static final Map<String, String> headerLinkMap = new HashMap<>();

    static {
        headerLinkMap.put("channelCode", "入链账号");
        headerLinkMap.put("channelType", "入链渠道");
        headerLinkMap.put("channelName", "入链账户名称");
        headerLinkMap.put("channelAgentNo", "入链账号所属机构网点");
    }

    private static final Map<String, String> headerActivationMap = new HashMap<>();

    static {
        headerActivationMap.put("agentNo", "激活客户经理归属机构号");
        headerActivationMap.put("agentName", "激活客户经理归属网点");
        headerActivationMap.put("employeeName", "激活客户经理名称");
        headerActivationMap.put("employeePhone", "激活客户经理手机号");
    }


    @Override
    public String exportActivityExcel(SysExportTaskEntity sysExportTaskEntity) {
        ActivityAwardDTO activityAwardDTO = JSONObject.parseObject(sysExportTaskEntity.getRequestParam(), ActivityAwardDTO.class);
        ActivityAwardEntity activityAwardEntity = activityAwardService.lambdaQuery()
                .eq(ActivityAwardEntity::getAwardId, activityAwardDTO.getAwardId())
                .eq(ActivityAwardEntity::getAwardType, activityAwardDTO.getAwardType()).one();
        ActivityEntity activity = activityService.lambdaQuery().eq(ActivityEntity::getActivityId, activityAwardEntity.getActivityId()).one();
        AwardSettingEntity awardSettingEntity = awardSettingService.lambdaQuery()
                .eq(AwardSettingEntity::getAwardType, activityAwardEntity.getAwardType())
                .eq(AwardSettingEntity::getAwardId,activityAwardEntity.getAwardId()).one();
        if (activity == null){
            throw new RuntimeException("网络波动,请联系管理员");
        }
        activityAwardDTO.setActivityId(activity.getActivityId());
        List<ExportAwardVO> list = activityService.activityAwardDetail(activityAwardDTO);
        if (activityAwardDTO.getAwardType() == 0L){
            Map<String, String> map = extracted(activity, awardSettingEntity, headerCouponMap);
            return createExcelAndUploadOssUrl(list, map, sysExportTaskEntity);
        }else if (activityAwardDTO.getAwardType() == 1L){
            Map<String, String> map = extracted(activity, awardSettingEntity, headerGoodsMap);
            return createExcelAndUploadOssUrl(list, map, sysExportTaskEntity);
        }else if (activityAwardDTO.getAwardType() == 2L){
            Map<String, String> map = extracted(activity, awardSettingEntity, headerVirtualMap);
            return createExcelAndUploadOssUrl(list, map, sysExportTaskEntity);
        }else if (activityAwardDTO.getAwardType() == 3L){
            Map<String, String> finalMap = new HashMap<>(headerScorcMap);
            if (activity.getThumbMediaId() != null && !activity.getThumbMediaId().isEmpty()){
                finalMap.putAll(headerLinkMap);
            }
            return createExcelAndUploadOssUrl(list, finalMap, sysExportTaskEntity);
        }

        throw new RuntimeException("网络波动,请联系管理员");
    }

    private static Map<String, String> extracted(ActivityEntity activity, AwardSettingEntity awardSettingEntity, Map<String, String> map) {
        Map<String, String> resultMap = new HashMap<>(map);
        if (activity.getThumbMediaId() != null && !activity.getThumbMediaId().isEmpty()){
            resultMap.putAll(headerLinkMap);
        }
        if (awardSettingEntity != null && awardSettingEntity.getIsNeedActivate() == 1){
            resultMap.putAll(headerActivationMap);
        }
        return resultMap;
    }
/**
 * 新版
 */
//    @Override
//    public String exportActivityExcel(SysExportTaskEntity sysExportTaskEntity) {
//        ActivityAwardDTO activityAwardDTO = JSONObject.parseObject(sysExportTaskEntity.getRequestParam(), ActivityAwardDTO.class);
//        ActivityAwardEntity activityAwardEntity = activityAwardService.lambdaQuery()
//                .eq(ActivityAwardEntity::getAwardId, activityAwardDTO.getAwardId())
//                .eq(ActivityAwardEntity::getAwardType, activityAwardDTO.getAwardType()).one();
//        ActivityEntity activity = activityService.lambdaQuery().eq(ActivityEntity::getActivityId, activityAwardEntity.getActivityId()).one();
//        AwardSettingEntity awardSettingEntity = awardSettingService.lambdaQuery()
//                .eq(AwardSettingEntity::getAwardType, activityAwardEntity.getAwardType())
//                .eq(AwardSettingEntity::getAwardId, activityAwardEntity.getAwardId()).one();
//        if (activity == null) {
//            throw new RuntimeException("网络波动,请联系管理员");
//        }
//        activityAwardDTO.setActivityId(activity.getActivityId());
//
//        // 获取总数据量
//        int totalCount = activityService.countActivityAwardDetail(activityAwardDTO);
//
//        if (activityAwardDTO.getAwardType() == 0L) {
//            Map<String, String> map = extracted(activity, awardSettingEntity, headerCouponMap);
//            return createExcelAndUploadOssUrlBatchMP(
//                    params -> {
//                        return activityService.activityAwardDetail(new Page(params[0], params[1]),activityAwardDTO);
//                    },
//                    totalCount,
//                    map,
//                    sysExportTaskEntity
//            );
//        } else if (activityAwardDTO.getAwardType() == 1L) {
//            Map<String, String> map = extracted(activity, awardSettingEntity, headerGoodsMap);
//            return createExcelAndUploadOssUrlBatchMP(
//                    params -> {
//                        return activityService.activityAwardDetail(new Page(params[0], params[1]),activityAwardDTO);
//                    },
//                    totalCount,
//                    map,
//                    sysExportTaskEntity
//            );
//        } else if (activityAwardDTO.getAwardType() == 2L) {
//            Map<String, String> map = extracted(activity, awardSettingEntity, headerVirtualMap);
//            return createExcelAndUploadOssUrlBatchMP(
//                    params -> {
//                        return activityService.activityAwardDetail(new Page(params[0], params[1]),activityAwardDTO);
//                    },
//                    totalCount,
//                    map,
//                    sysExportTaskEntity
//            );
//        } else if (activityAwardDTO.getAwardType() == 3L) {
//            Map<String, String> finalMap = new HashMap<>(headerScorcMap);
//            if (activity.getThumbMediaId() != null && !activity.getThumbMediaId().isEmpty()) {
//                finalMap.putAll(headerLinkMap);
//            }
//            return createExcelAndUploadOssUrlBatchMP(
//                    params -> {
//                        return activityService.activityAwardDetail(new Page(params[0], params[1]),activityAwardDTO);
//                    },
//                    totalCount,
//                    finalMap,
//                    sysExportTaskEntity
//            );
//        }
//
//        throw new RuntimeException("网络波动,请联系管理员");
//    }
}
